package model.mediators;

import java.util.Iterator;

import model.entity.Entity;
import model.item.Inventory;
import model.item.InventoryItem;

public class UseItemMediator extends InteractionMenuMediator {
	
	private Inventory inventory;
	private Entity target;
	private InteractiveMediator parentMediator; 
	
	/**
	 * @param inventory
	 * @param target
	 */
	public UseItemMediator(Inventory inventory, Entity target, InteractiveMediator parentMediator) {
		this.inventory = inventory;
		this.target = target;
		this.parentMediator = parentMediator;
		init();
	}
	
	private class UseItemInteractionOption extends SimpleInteractionOption{
		
		InventoryItem item;
		UseItemInteractionOption(String name, InventoryItem item) {
			super(name);
			this.item = item;
		}
		
		public void actionPerformed() {
			inventory.removeItem(item);
			item.use(target);
		}
	}
	public void init() {
		WindowedInteraction window = new WindowedInteraction("Use Item Menu", this);
		
		Iterator<InventoryItem> inventoryIterator = inventory.getInventory();
		
		while (inventoryIterator.hasNext()) {
			InventoryItem item = inventoryIterator.next();
			window.addButton(new UseItemInteractionOption(item.getName(), item));
		}
		
		window.createWindow();
	}
	
	public void exit() {
			parentMediator.show();
	}
	
}
